package com.pkx.cloud.fse7.dms.staff.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pkx.cloud.fse7.dms.staff.entity.Staff;
import com.pkx.cloud.fse7.dms.staff.feign.StaffFeign;
import com.pkx.cloud.fse7.dms.staff.repo.StaffMapper;
import com.pkx.cloud.fse7.dms.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.StringJoiner;

/**
 * @Description
 * @Author chenwg
 * @date 2020.11.11 13:10
 */
@Service
@Slf4j
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {
    @Autowired
    private StaffFeign staffFeign;
    @Value(value = "${pkx.Gid}")
    private String Gid;

    @Override
    @Async
    public String addStaff(Staff staff) {
        staff.setId(null);
        staff.setGid(Gid);
        staff.setRid(staff.getStaffNo());
        log.info("用户新增参数：{}", JSON.toJSONString(staff));
        return staffFeign.EditStaff(staff);
    }

    @Override
    @Async
    public String editStaff(Staff staff) {
        // 根据用户id 查询用户详细信息
        LambdaQueryWrapper<Staff> eq = Wrappers.lambdaQuery(Staff.class)
                .eq(Staff::getStaffNo, staff.getStaffNo());
        Staff one = getOne(eq);
        // 如果用户不存在 则新增
        if (!Objects.isNull(one)) {
            staff.setId(one.getId());
        }
        one.setStaffName(staff.getStaffName());
        one.setOrganizationId(staff.getOrganizationId());
        return staffFeign.EditStaff(one);
    }

    @Override
    @Async
    public String removeStaff(String StaffNo) {
        LambdaQueryWrapper<Staff> eq = Wrappers.lambdaQuery(Staff.class)
                .eq(Staff::getStaffNo, StaffNo);
        Staff one = getOne(eq);
        if (!Objects.isNull(one)) {
            Long id = one.getId();
            StringJoiner stringJoiner = new StringJoiner("=", "\"(", ")\"");
            stringJoiner.add("ID")
                    .add(id.toString());
            return staffFeign.DeleteFunc(stringJoiner.toString());
        }
        return null;
    }


}

